import { fetchAreaListAPI, fetchRuleListAPI, deleteAreaAPI, addAreaAPI, updateAreaAPI } from '@/api/car/area'

const actions = {
  /* 发送请求获取区域列表的action */
  async fetchAreaList(context, pageParams) {
    const { data: areaListInfo } = await fetchAreaListAPI(pageParams)
    context.commit('updateAreaList', areaListInfo)
  },
  /* 发送请求添加区域的action */
  async addArea(context, formData) {
    const { msg } = await addAreaAPI(formData)
    return msg
  },
  /* 发送请求编辑区域的action */
  async updateArea(context, formData) {
    const { msg } = await updateAreaAPI(formData)
    return msg
  },
  /* 发送请求删除区域的action */
  async deleteArea(context, id) {
    const { msg } = await deleteAreaAPI(id)
    return msg
  },
  /* 发送请求获取关联计费规则的action */
  async fetchRuleList(context) {
    const { data: { rows } } = await fetchRuleListAPI()
    context.commit('updateRuleList', rows)
  }
}
const mutations = {
  /* 更新区域列表的mutation */
  updateAreaList(state, areaListInfo) {
    state.areaListInfo = areaListInfo
  },
  /* 更新关联计费规则的mutation */
  updateRuleList(state, ruleList) {
    state.ruleList = ruleList
  }
}
const state = () => {
  return {
    areaListInfo: {},
    ruleList: []
  }
}
const getters = {
  areaList(state) {
    return state.areaListInfo.rows
  },
  total(state) {
    return state.areaListInfo.total
  }
}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
